<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pokes_mod extends CI_Model
{   
    
    private $_dbTablePokes = 'pokes';
    private $_dbTableUsers = 'users';
    private $_dbTableImages = 'images';
    /**
     *  class constructor
     */
    function __construct() 
	{
		// Call the Model constructor
        parent::__construct();
	}
    
    public function getMyPokesList($id)
    {
        
        $sql = "SELECT {$this->_dbTablePokes}.id,
                       {$this->_dbTablePokes}.user_id,
                       {$this->_dbTablePokes}.poke_id,
                       {$this->_dbTablePokes}.reply,
                       {$this->_dbTablePokes}.timestamp,
                         
                       {$this->_dbTableUsers}.username,
                       {$this->_dbTableUsers}.name,
                       {$this->_dbTableUsers}.flirt_text,
                       ( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and `user_id`= {$this->_dbTablePokes}.user_id and main=1 LIMIT 1) as image_name
                FROM  {$this->_dbTablePokes}, {$this->_dbTableUsers}
                WHERE {$this->_dbTablePokes}.user_id = {$this->_dbTableUsers}.id
                      AND {$this->_dbTablePokes}.poke_id = $id
                      AND is_del = 0
                ORDER BY {$this->_dbTablePokes}.timestamp DESC;
               ";
        $query = $this->db->query($sql);
        
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }

        if ($query->num_rows() > 0) {
          $result = $query->result();
          $query->free_result();
        } else {
          $result = NULL;
        }

        return $result;
    }
    
    /**
     *  return number non pushback pokes
     */
    public function getNumNonReplyPokes()
    {
        $this->db->from($this->_dbTablePokes)
                 ->where('poke_id',$this->session->userdata('user_id'))// koje su meni poslate
                 ->where('reply',0) // A nisam odgovorio na njih
                 ->where('is_del',0); // i nisu obrisani
        $num_unread_pokes = $this->db->count_all_results();
        
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }
       
        return $num_unread_pokes;
    }
    
    /**
     *  AJAX
     */
    
    public function send_poke($poke_id)
    {
        $from = $this->session->userdata('user_id'); // Logovan korisnik salje POKE
        // Prvo proveri da li ima para za POKE
        $query = $this->db->select('coins')
                          ->where('id',$from)
                          ->get($this->_dbTableUsers);
        
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }
        
        if ($query->num_rows() == 1) {
          $coins = $query->row()->coins;          
          $query->free_result();          
        } else {
          //$coins = 0;
          return false;
        }        
        $poke_cost = $this->config->item('poke_cost');
        if( $coins >= $poke_cost )
        {
          // Smanji kredit 
          $sql = "UPDATE {$this->_dbTableUsers} SET `coins`=`coins`-{$poke_cost} WHERE `id`={$from}; ";
          $query = $this->db->query($sql);
          
          $errors = $this->db->_error_message();
          if (!empty($errors)) {
            throw new Exception("SQL Error: " . $errors);
          } else { }
          
          if ($this->db->affected_rows() != 1) {
              return false;
              //$query->free_result();
          } else { }
        }
        else
        {
          return -2; // Nema para za poke
        }
        
      
        $data = array(
                    'user_id'=>$this->session->userdata('user_id'),
                    'poke_id' => $poke_id
                );
        $this->db->insert($this->_dbTablePokes,$data);
          
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
          return false;
        } else { }
        
        if( $this->db->affected_rows() == 1 ) {
          
          // Update poke
          $data = array('reply'=>1);
          $this->db->where('user_id',$poke_id)
                   ->where('poke_id',$this->session->userdata('user_id'))
                   ->update($this->_dbTablePokes,$data);
          //echo $this->db->last_query();
          $result = 1;          
        }
        else {
          $result = false;
        }
        
        return $result;
    }

    public function remove_poke($poke_id)
    {        
        $data = array("is_del" => 1);
        $this->db->where('id',$poke_id);
        //$this->db->where('user_id',$this->session->userdata('user_id'));
        //$this->db->delete($this->_dbTablePokes);
        $this->db->update($this->_dbTablePokes,$data);
        
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
          return false;
        } else { }
        
        if( $this->db->affected_rows() == 1 )
        {
           return true;
        }
        else 
        {
          return false;
        }
    }
    
    
}// end class Pokes_mod

/*
      $query = $this->db->select('*')
                        ->from()
                        ->where()
                        ->get();
      //echo $this->db->last_query();
      $errors = $this->db->_error_message();
      if (!empty($errors)) {
        throw new Exception("SQL Error: " . $errors);
      } else { }

      if ($query->num_rows() > 0) {
        $result = $query->result();
        $query->free_result();
      } else {
        $result = NULL;
      }

      return $result;
 */

// end file: application/models/pokes_mod.php

?>